package com.jy.oms.framework.asyn.task.model.strategy;

import com.jy.oms.framework.asyn.task.constants.TaskExecuteStrategyEnum;
import com.jy.oms.framework.asyn.task.constants.TimeUnitEnum;
import com.jy.oms.framework.asyn.task.exception.TaskConfigException;

/**
 * 限速执行策略
 *
 * @author liuquan
 */
public class LimiterExecuteStrategy extends ExecuteStrategyObj {

    /**
     * 是否可动态调整时间单位、时间间隔、执行次数;
     * 为true时，可在Nacos里配置相关参数
     */
    private boolean adjustable;

    /**
     * 时间单位
     */
    private TimeUnitEnum timeUnit = TimeUnitEnum.Hour;

    /**
     * 时间间隔
     */
    private Integer interval = 1;

    /**
     * 限制执行次数
     */
    private Integer limiter = 3;

    public LimiterExecuteStrategy(String taskCode, TaskExecuteStrategyEnum taskExecuteStrategy) {
        super(taskCode, taskExecuteStrategy);
    }

    public Integer toSeconds() {
        if (timeUnit == TimeUnitEnum.Second) {
            return interval;
        } else if (timeUnit == TimeUnitEnum.Minute) {
            return interval * 60;
        } else if (timeUnit == TimeUnitEnum.Hour) {
            return interval * 60 * 60;
        } else if (timeUnit == TimeUnitEnum.Day) {
            return interval * 24 * 60 * 60;
        }

        throw new TaskConfigException("不支持时间单位转化");
    }

    public boolean isAdjustable() {
        return adjustable;
    }

    public LimiterExecuteStrategy setAdjustable(boolean adjustable) {
        this.adjustable = adjustable;
        return this;
    }

    public TimeUnitEnum getTimeUnit() {
        return timeUnit;
    }

    public LimiterExecuteStrategy setTimeUnit(TimeUnitEnum timeUnit) {
        this.timeUnit = timeUnit;
        return this;
    }

    public Integer getInterval() {
        return interval;
    }

    public LimiterExecuteStrategy setInterval(Integer interval) {
        this.interval = interval;
        return this;
    }

    public Integer getLimiter() {
        return limiter;
    }

    public LimiterExecuteStrategy setLimiter(Integer limiter) {
        this.limiter = limiter;
        return this;
    }
}
